// pages/mine/editgrade/editgrade.js
import Toast from '../../../miniprogram_npm/@vant/weapp/toast/toast';
import {
    GetStudentgrades, // 我的成绩查询
    GetStudentCslist, // 我的成绩可修改次数
    GetStudentgradesAdd, // 我的成绩保存
    GetLkFdbyscore, // 根据分数获取理科位次
    GetWkFdbyscore, // 根据分数获取文科位次
    GetSwitchconfig, // 开关接口
} from '../../../api/index'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        dataSourceArray: [{
                title: '高考省份',
                content: '黑龙江',
                unit: '',
                placeholder: ''
            },
            {
                title: '填报批次',
                content: '本科批',
                unit: '',
                placeholder: ''
            },
            {
                title: '高考科类',
                content: '请选择科类',
                unit: '',
                placeholder: ''
            },
            {
                title: '考试分数',
                content: '',
                unit: '分',
                placeholder: '请输入分数'
            },
            {
                title: '对应位次',
                content: '',
                unit: '位',
                placeholder: '自动获取'
            }
        ],
        subjectOne: ['物理', '历史'],
        subjectTwo: ['化学', '生物', '政治', '地理'],
        selectOne: '',
        selectTwo: [],
        makeSelectOne: '',
        makeSelectTwo: [],
        fill_show: false,
        score: '', // 分数 
        position: '', // 位次
        gradeArray:[],
        switchStatus:1, // 是否可以保存多次 0 可以 1不可以
        lot:'本科批',
        makeLot:'本科批',
        lot_show:false,
        submit:false, // 是否保存成绩了，防止连点
        changeNumber:0, // 可修改次数
        year:'',
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        const now = new Date();
        const year = now.getFullYear(); // 获取当前年份
        console.log(year)
        this.setData({
            year:year
        })

        this._GetSwitchconfig('', false)
        let openid = wx.getStorageSync('openId') || '';
        this._GetStudentCslist('?openId=' + openid, true)
        this._GetStudentgrades('?openId=' + openid, true)
    },

    // 输入框输入事件
    inputEvent(event) {
        console.log(event)
        let index = event.currentTarget.dataset.row
        let value = event.detail.value
        if (index == 3) {
            this.setData({
                score: value
            })
        } else if (index == 4) {
            this.setData({
                position: value
            })
        }
        this.setData({
            ['dataSourceArray[' + index + '].content']: value
        })
        if (this.data.makeSelectOne == '物理') { // 理科
            this._GetLkFdbyscore({zf:value}, false)
        } else {
            this._GetWkFdbyscore({zf:value}, false)
        }
    },

    // 输入框失去焦点
    handleInputComplete(event) {
        // let index = event.target.dataset.row;
        // let value = event.detail.value;
        // console.log(event)
        // if (index == 3) {
        //     return
        // }
        // if (this.data.makeSelectOne == '物理') { // 理科
        //     this._GetLkFdbyscore({zf:value}, false)
        // } else {
        //     this._GetWkFdbyscore({zf:value}, false)
        // }
    },

    // 关闭弹窗
    close() {
        this.setData({
            fill_show: false,
            lot_show:false
        })
    },
    // 列表点击事件
    cellSelectAction(event) {
        let index = event.currentTarget.dataset.index
        let that = this
        if (index == 1) {
            this.setData({
                lot:that.data.makeLot,
                lot_show:true
            })
        }
        // if (this.data.switchStatus == 1 && this.data.gradeArray.length != 0) {
        //     return
        // }
        if (this.data.changeNumber == 0) {
            return
        }
        // if (this.data.switchStatus == 1 && this.data.position != null) {
        //     return
        // }
        if (index == 2) {
            this.setData({
                selectOne: that.data.makeSelectOne,
                selectTwo: that.data.makeSelectTwo,
                fill_show: true
            })
        }
    },

    // 批次点击事件
    lotActioon(event) {
        console.log(event)
        this.setData({
            lot:event.currentTarget.dataset.item
        })
    },

    // 批次确定
    makeLotAction() {
        console.log(this.data.lot)
        let that = this;
        this.setData({
            makeLot:that.data.lot,
            ['dataSourceArray[' + 1 + '].content']: that.data.lot,
            lot_show:false
        })
    },

    // 首选科目点击事件
    subjectOneActioon(event) {
        console.log(event)
        let value = event.currentTarget.dataset.item
        this.setData({
            selectOne: value
        })
    },

    // 再选科目
    subjectTwoActioon(event) {
        console.log(event)
        let value = event.currentTarget.dataset.item;
        let that = this;

        if (this.data.selectTwo.indexOf(value) > -1) {
            this.setData({
                selectTwo: that.data.selectTwo.filter(item => item !== value)
            })
        } else {
            let array = this.data.selectTwo;
            if (array.length >= 2) {
                console.log(array)
                array = array.slice(1);
                console.log(array)
                console.log('1')
                array.push(value)
            } else {
                console.log('2')
                array.push(value)
            }
            this.setData({
                selectTwo: array
            })
        }
        console.log(this.data.selectTwo)
    },

    // 确认选择科目
    makeAction() {
        console.log(this.data.selectOne)
        if (this.data.selectOne == '') {
            Toast('请选择1个首选科目')
            return
        }
        if (this.data.selectTwo.length < 2) {
            Toast('请选择2个再选科目')
            return
        }
        let that = this;
        this.setData({
            makeSelectOne: that.data.selectOne,
            makeSelectTwo: that.data.selectTwo,
            fill_show: false
        })
        let str = this.data.makeSelectTwo.join('/'); // 使用逗号分隔
        let content = this.data.makeSelectOne + '/' + str
        this.setData({
            ['dataSourceArray[' + 2 + '].content']: content
        })
        console.log('啥')
        console.log(this.data.score)
        if (this.data.score != '') {
            if (this.data.makeSelectOne == '物理') { // 理科
                this._GetLkFdbyscore({zf:this.data.score}, false)
            } else {
                this._GetWkFdbyscore({zf:this.data.score}, false)
            }
        }
    },

    // 保存成绩
    saveScoreAction() {
        console.log(this.data.selectOne)
        console.log(this.data.selectTwo)
        if (this.data.submit == true) {
            return
        }

        if (this.data.selectOne == '') {
            Toast('请选择科目')
            return
        }
        if (this.data.selectTwo.length == 0) {
            Toast('请选择科目')
            return
        }
        if (this.data.score == '') {
            Toast('请填写分数')
            return
        }
        if (this.data.score > 750) {
            Toast('最大分数不能超过750')
            return
        }
        if (this.data.position == '' || this.data.position == null) {
            Toast('位次获取失败，请检查分数以及高考科类是否正确')
            return
        }

        let isWuli = this.data.selectOne == '物理' ? 1 : 0
        let isLishi = this.data.selectOne == '历史' ? 1 : 0
        var isHuaxue = ''
        var isShengwu = ''
        var isZhengzhi = ''
        var isDili = ''
        if (this.data.selectTwo.indexOf('化学') > -1) {
            isHuaxue = 1
        } else {
            isHuaxue = 0
        }
        if (this.data.selectTwo.indexOf('生物') > -1) {
            isShengwu = 1
        } else {
            isShengwu = 0
        }
        if (this.data.selectTwo.indexOf('政治') > -1) {
            isZhengzhi = 1
        } else {
            isZhengzhi = 0
        }
        if (this.data.selectTwo.indexOf('地理') > -1) {
            isDili = 1
        } else {
            isDili = 0
        }
        let openid = wx.getStorageSync('openId') || '';
        var id = ''
        var createTime = ''
        if (this.data.gradeArray.length != 0) {
            id = this.data.gradeArray[0].id
            createTime = this.data.gradeArray[0].createTime
        }
        console.log({
            id:id,
            openId: openid,
            isWuli: isWuli,
            isLishi: isLishi,
            isHuaxue: isHuaxue,
            isShengwu: isShengwu,
            isZhengzhi: isZhengzhi,
            isDili: isDili,
            myScore: this.data.score,
            myWeici: this.data.position,
            createTime:createTime,
            reserve1:this.data.makeLot
        })
        this.setData({
            submit:true
        })
        this._GetStudentgradesAdd({
            id:id,
            openId: openid,
            isWuli: isWuli,
            isLishi: isLishi,
            isHuaxue: isHuaxue,
            isShengwu: isShengwu,
            isZhengzhi: isZhengzhi,
            isDili: isDili,
            myScore: this.data.score,
            myWeici: this.data.position,
            createTime:createTime,
            reserve1:this.data.makeLot
        }, true);
    },

    _GetStudentCslist(data, isLoading) {
        GetStudentCslist(data, isLoading).then(res => {
            console.log(res)
            let code = res.code;
            if (code == 200) {
                let array = res.rows;
                if (array.length != 0) {
                    this.setData({
                        changeNumber:array[0].reserve2,
                    })
                } else {
                    this.setData({
                        changeNumber:199
                    })
                }
                
            }
        })
    },

    // 我的成绩查询
    _GetStudentgrades(data, isLoading) {
        GetStudentgrades(data, isLoading).then(res => {
            console.log(res)
            let code = res.code;
            // Toast('成功')
            if (code == 200) {
                let array = res.rows;
                this.setData({
                    gradeArray:array
                })
                if (array.length != 0) {
                    let model = array[0]
                    if (model.isWuli == 1) {
                        this.setData({
                            selectOne: '物理',
                            makeSelectOne:'物理',
                        })
                    } else {
                        this.setData({
                            selectOne: '历史',
                            makeSelectOne:'历史'
                        })
                    }
                    this.setData({
                        selectTwo:[],
                        makeSelectTwo:[]
                    })
                    if (model.isHuaxue == 1) {
                        let array = this.data.selectTwo;
                        array.push('化学')
                        this.setData({
                            selectTwo: array,
                            makeSelectTwo: array
                        })
                    }
                    if (model.isShengwu == 1) {
                        let array = this.data.selectTwo;
                        array.push('生物')
                        this.setData({
                            selectTwo: array,
                            makeSelectTwo: array
                        })
                    }
                    if (model.isZhengzhi == 1) {
                        let array = this.data.selectTwo;
                        array.push('政治')
                        this.setData({
                            selectTwo: array,
                            makeSelectTwo: array
                        })
                    }
                    if (model.isDili == 1) {
                        let array = this.data.selectTwo;
                        array.push('地理')
                        this.setData({
                            selectTwo: array,
                            makeSelectTwo: array
                        })
                    }
                    let str = this.data.makeSelectTwo.join('/'); // 使用逗号分隔
                    let content = this.data.makeSelectOne + '/' + str
                    this.setData({
                        
                        score:model.myScore,
                        ['dataSourceArray[' + 1 + '].content']: model.reserve1,
                        ['dataSourceArray[' + 2 + '].content']: content,
                        ['dataSourceArray[' + 3 + '].content']: model.myScore,
                        ['dataSourceArray[' + 4 + '].content']: model.myWeici,
                        position:model.myWeici,
                        makeLot:model.reserve1,
                    })
                    console.log(this.data.dataSourceArray)
                }
            } else {
                // Toast(res.msg)
            }
        })
    },

    // 我的成绩保存
    _GetStudentgradesAdd(data, isLoading) {
        GetStudentgradesAdd(data, isLoading).then(res => {
            console.log(res)
            let code = res.code;
            // Toast('成功')
            if (code == 200) {
                this.setData({
                    submit:false
                })
                Toast({
                    type: 'success',
                    message: '保存成功',
                    onClose: () => {
                        //保存成功返回上一页 获取上一个页面的实例对象
                        var pages = getCurrentPages()
                        // 拿到上一个页面的索引
                        var previousPage = pages[pages.length - 2];
                        wx.navigateBack({ delta: 1 })
                            // 调用上一页的 nav_page方法
                        previousPage.navpage()
                    },
                  });
            } else {
                Toast(res.msg)
            }
        })
    },

    // 根据分数获取理科位次
  _GetLkFdbyscore(data, isLoading) {
    GetLkFdbyscore(data, isLoading).then(res => {
        console.log(res)
        let code = res.code;
        // Toast('成功')
        if (code == 200) {
            this.setData({
                position:res.data.fd,
                ['dataSourceArray[' + 4 + '].content']: res.data.fd
            })
            
        }
    })
},

  // 根据分数获取文科位次
  _GetWkFdbyscore(data, isLoading) {
    GetWkFdbyscore(data, isLoading).then(res => {
        console.log(res)
        let code = res.code;
        // Toast('成功')
        if (code == 200) {
            this.setData({
                position:res.data.fd,
                ['dataSourceArray[' + 4 + '].content']: res.data.fd
            })
        } 
    })
},

_GetSwitchconfig(data, isLoading) {
    GetSwitchconfig(data, isLoading).then(res => {
        console.log(res)
        let that = this;
        let code = res.code;
        // Toast('成功')
        if (code == 200) {
            let array = res.rows;
            for (const item of array) {
                if (item.code =="myscore") {
                    that.setData({
                        switchStatus:item.switchStatus
                    })
                    return
                }
            }
        } else {
            Toast(res.msg)
        }
    })
},

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    onShareTimeline: function (){

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})